package com.test.license;


import cn.hutool.core.date.DateUtil;

import java.io.Serializable;
import java.util.Date;
import java.util.Properties;


/**
 * @author: zouren
 * @date: 2022/3/8
 * @description:
 */
public class License implements Serializable {
    public static final String LICENSE_TYPE_DEMO = "Demo";
    public static final String LICENSE_TYPE_BASE = "Base";
    public static final String LICENSE_TYPE_PROD = "Prod";

    public static final String KEY_APPID = "appId";
    public static final String KEY_PRODUCT = "product";
    public static final String KEY_VERSION = "version";
    public static final String KEY_LICENSETYPE = "licenseType";
    public static final String KEY_TRYDAYS = "tryDays";
    public static final String KEY_EXPIRESDATE = "expiresDate";
    public static final String KEY_INSTALLDATE = "installDate";
    public static final String KEY_SIGNATURE = "signature";


    /**
     * 应用id
     */
    private String appId;
    /**
     * 应用名
     */
    private String product;
    /**
     * 版本
     */
    private String version;
    /**
     * 许可证类型
     * Demo Base Prod
     */
    private String licenseType;
    /**
     * 用户名
     */
    private String userName;
    /**
     * 公司名
     */
    private String company;
    /**
     * 用户数
     */
    private int onlineUser;
    /**
     * 使用次数
     */
    private int deviceNum;
    /**
     * 试用日期，默认为30天
     */
    private int tryDays;
    /**
     * 过期日期
     */
    private String expiresDate;
    /**
     * 安装日期
     */
    private String installDate;
    /**
     * 签名
     */
    private String signature;

    public License() {

    }

    public void init(Properties config) {
        this.appId = config.getProperty(KEY_APPID);
        this.product = config.getProperty(KEY_PRODUCT);
        this.version = config.getProperty(KEY_VERSION);
        this.licenseType = config.getProperty(KEY_LICENSETYPE,LICENSE_TYPE_DEMO);
        this.installDate = config.getProperty(KEY_INSTALLDATE, DateUtil.today());
        int empTryDays = 30;
        if (LICENSE_TYPE_DEMO.equals(this.licenseType)) {
            empTryDays = 30;
            onlineUser = 5;
            deviceNum = 1;
        } else if (LICENSE_TYPE_BASE.equals(this.licenseType)) {
            empTryDays = 365;
            onlineUser = 50;
            deviceNum = 10;
        } else if (LICENSE_TYPE_PROD.equals(this.licenseType)) {
            empTryDays = 36500;
            onlineUser = 500;
            deviceNum = Integer.MIN_VALUE;
        }
        this.tryDays = Integer.parseInt(config.getProperty(KEY_TRYDAYS,empTryDays+""));
        this.expiresDate= config.getProperty(KEY_EXPIRESDATE, DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parse(installDate), tryDays)));
        this.signature = config.getProperty(KEY_SIGNATURE);

    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getProduct() {
        return product;
    }

    public void setProduct(String product) {
        this.product = product;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getLicenseType() {
        return licenseType;
    }

    public void setLicenseType(String licenseType) {
        this.licenseType = licenseType;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public int getOnlineUser() {
        return onlineUser;
    }

    public void setOnlineUser(int onlineUser) {
        this.onlineUser = onlineUser;
    }

    public int getDeviceNum() {
        return deviceNum;
    }

    public void setDeviceNum(int deviceNum) {
        this.deviceNum = deviceNum;
    }

    public int getTryDays() {
        return tryDays;
    }

    public void setTryDays(int tryDays) {
        this.tryDays = tryDays;
    }
    public String getExpiresDateStr() {
        return expiresDate;
    }
    public Date getExpiresDate() {
        Date date = DateUtil.parse(expiresDate);
        return DateUtil.endOfDay(date);
    }

    public void setExpiresDate(String expiresDate) {
        this.expiresDate = expiresDate;
    }
    public String getInstallDateStr() {
        return installDate;
    }
    public Date getInstallDate() {
        Date date = DateUtil.parse(installDate);
        return DateUtil.endOfDay(date);
    }

    public String getSignature() {
        return signature;
    }

    public void setInstallDate(String installDate) {
        this.installDate = installDate;
    }

    @Override
    public String toString() {
        return "License{" +
                "appId='" + appId + '\'' +
                ", product='" + product + '\'' +
                ", version='" + version + '\'' +
                ", licenseType='" + licenseType + '\'' +
                ", userName='" + userName + '\'' +
                ", company='" + company + '\'' +
                ", onlineUser=" + onlineUser +
                ", deviceNum=" + deviceNum +
                ", tryDays=" + tryDays +
                ", expiresDate='" + expiresDate + '\'' +
                ", installDate='" + installDate + '\'' +
                '}';
    }
}
